const express = require('express');
const path = require('path');
const bodyparser = require('body-parser'); //body中间件
const adminRouter = require('./router/admin.js'); //管理菜单等路由
const wxRouter = require('./router/wxapp.js'); //小程序等路由
const cors = require('cors'); //解决跨域的中间件
const server = express();
const {errLog}= require("./utils/err");
const utils = require("./utils/index.js");




server.listen(9090);



// 跨域请求
server.use(cors({origin: "*",}));
// server.use(express.static('./uploads')); //用户的静态资源
// 限制接口上传数据的大小
server.use(bodyparser.json({limit: '5000mb'}));

server.use(express.json({limit: '50mb'}));
// server.use(express.urlencoded({limit: '50mb'}));
// server.use(bodyParser.urlencoded({limit: '5000mb', extended: true}));

// // 设置静态文件目录，用于访问上传的图片
// server.use('/uploads', express.static(path.join(__dirname, 'uploads')));

// server.use(bodyparser.urlencoded({//body中间件
// 	extended:false
// }));
server.use(async function (req, res, next)  {
	if(req.headers.token){
		let user=await utils.getUserInfo(req,res);
		if(user.status===0) return res.send(utils.returnData({code: 203, msg: "你账号已被禁用，请联系管理员！！",req}));
	}
	next();
})


// // 错误日志模块
// process.on('unhandledRejection', (err, test) => {
// 	errLog({err,code:500,msg:"后端系统错误！",funName:"fatal"});
// 	}).on('uncaughtException', err => {
// 	errLog({err,code:500,msg:"后端系统错误！！",funName:"fatal"});
// 	});





server.use('/api/v1/admin', adminRouter); //挂载用户信息路由
server.use('/api/v1/wx', wxRouter); //挂载小程序信息路由

//静态目录 https://gcplab.gxnu.edu.cn/yl/api/v1/uploads/
// server.use('/uploads', express.static('./uploads'));
server.use('/api/v1/uploads', express.static(path.join(__dirname, 'uploads')));






console.log('后端接口启动成功');

// https://colab.research.google.com/


// https://www.cnblogs.com/hiit/p/11922348.html

// # 重启 Nginx 服务
// sudo systemctl restart nginx

// # 检查重启状态
// sudo systemctl status nginx

// 迁移前端页面
///usr/share/nginx/html
// rm -rf  /usr/share/nginx/html/admin1/*
// cp -rf dist/* /usr/share/nginx/html/admin1